<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * Script Name: Onmessage.php
 * Create: 2020/7/1 13:57
 * Description: 回调事件处理
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mp\controller;

use ky\SaveParam;
use think\Controller;

class Onmessage extends Controller
{
    protected $mpApp;
    protected $mpInfo;
    /**
     * @var \think\Model
     */
    protected $mpM;
    protected $mpId;

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        model('common/setting')->settings();
        $this->mpM = model('common/mp');
        $this->setMpId();
        $this->setMpInfo();
        $this->setApp();
    }

    /**
     * 设置公众号id
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function setMpId(){
        $this->mpId = input('mid', 0, 'intval');
    }

    /**
     * 设置微信公众号信息
     * @author fudaoji<fdj@kuryun.cn>
     */
    protected function setMpInfo() {
        $this->mpInfo = $this->mpM->getOne($this->mpId);
        if(empty($this->mpInfo)){
            echo("mid参数错误");exit;
        }
    }

    /**
     * 设置授权公众号应用
     * @author fudaoji<fdj@kuryun.cn>
     */
    protected function setApp() {
        if($this->mpInfo) {
            $this->mpApp = controller('mp/mp', 'event')->getApp($this->mpInfo);
        }
    }

    /**
     * 微信网页授权
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function wxAuthCallback(){
        $oauth = $this->mpApp->oauth;
        //获取OAuth授权结果用户信息
        $user = $oauth->user()->toArray();
        $original = $user['original'];

        $original_data = [
            'mpid'          => $this->mpId,
            'openid'        => $original['openid'],
            'nickname'      => $original['nickname'],
            'sex'           => $original['sex'],
            'headimgurl'    => $original['headimgurl'],
            'language'      => $original['language'],
            'city'          => $original['city'],
            'province'      => $original['province'],
            'country'       => $original['country'],
            'unionid'       => empty($original['unionid']) ? $original['openid'] : $original['unionid']
        ];

        if(! $follow = model('common/mpFollow')->getOneByMap(['mpid' => $this->mpId, 'openid' => $original_data['openid']])){
             model('common/mpFollow')->addOne($original_data);
        }

        cookie('weixin_user', SaveParam::authCode(json_encode($original_data), 'ENCODE'), 604800); //保存用户信息
        $target_url = empty(cookie('target_url')) ? url('index/index') : cookie('target_url');
        $this->redirect($target_url);
    }
}